using System.Collections.Generic;
using OracleInternal.TTC;

namespace OracleInternal.BulkCopy
{
	internal class DirectPathRowPiece
	{
		private static Queue<DirectPathRowPiece> s_directPathRowPieceCache;

		private static object s_directPathRowPieceCacheLock;

		internal int m_totalColsLen;

		internal int m_firstColIndex;

		internal int m_beginColDataOffset;

		internal int m_endColIndex;

		internal int m_endColDataOffset;

		internal bool m_bFastRow;

		internal bool m_bFullRow;

		internal bool m_bFirstPiece;

		internal bool m_bLastPiece;

		internal bool m_bContinueFromPrevPiece;

		internal bool m_bContinueToNextPiece;

		internal int m_colCount;

		private static int m_count;

		internal short Flags
		{
			get
			{
				short num = 0;
				if (m_bFastRow && m_bFullRow)
				{
					return 60;
				}
				if (m_bFirstPiece)
				{
					num = (short)(num | 8);
				}
				else if (m_bContinueFromPrevPiece)
				{
					num = (short)(num | 2);
				}
				if (m_bLastPiece)
				{
					num = (short)(num | 4);
				}
				else if (m_bContinueToNextPiece)
				{
					num = (short)(num | 1);
				}
				return num;
			}
		}

		internal int RowPieceLen
		{
			get
			{
				if (Flags == 60)
				{
					return m_totalColsLen + 4;
				}
				return m_totalColsLen + 2;
			}
		}

		static DirectPathRowPiece()
		{
			s_directPathRowPieceCache = new Queue<DirectPathRowPiece>();
			s_directPathRowPieceCacheLock = new object();
		}

		internal static void AddToCache(DirectPathRowPiece dpRowPiece)
		{
			lock (s_directPathRowPieceCacheLock)
			{
				s_directPathRowPieceCache.Enqueue(dpRowPiece);
			}
		}

		internal static void AddToCache(List<DirectPathRowPiece> dpRowPieces)
		{
			lock (s_directPathRowPieceCacheLock)
			{
				for (int i = 0; i < dpRowPieces.Count; i++)
				{
					s_directPathRowPieceCache.Enqueue(dpRowPieces[i]);
				}
			}
		}

		internal static DirectPathRowPiece GetFromCache()
		{
			lock (s_directPathRowPieceCacheLock)
			{
				if (s_directPathRowPieceCache.Count > 0)
				{
					return s_directPathRowPieceCache.Dequeue();
				}
				return new DirectPathRowPiece();
			}
		}

		private DirectPathRowPiece()
		{
		}

		internal static DirectPathRowPiece CreateFullPiece(bool bAllFastColumns, int len, int firstColIndex, int firstColDataOffset, int endColIndex, int endColDataOffset)
		{
			DirectPathRowPiece fromCache = GetFromCache();
			fromCache.InitFullPiece(bAllFastColumns, len, firstColIndex, firstColDataOffset, endColIndex, endColDataOffset);
			return fromCache;
		}

		internal static DirectPathRowPiece CreateFirstPiece(bool bAllFastColumns, int len, int firstColIndex, int firstColDataOffset, int endColIndex, int endColDataOffset)
		{
			DirectPathRowPiece fromCache = GetFromCache();
			fromCache.InitFirstPiece(bAllFastColumns, len, firstColIndex, firstColDataOffset, endColIndex, endColDataOffset);
			return fromCache;
		}

		internal static DirectPathRowPiece CreateNextPiece(bool bAllFastColumns, int len, int firstColIndex, int firstColDataOffset, int endColIndex, int endColDataOffset)
		{
			DirectPathRowPiece fromCache = GetFromCache();
			fromCache.InitNextPiece(bAllFastColumns, len, firstColIndex, firstColDataOffset, endColIndex, endColDataOffset);
			return fromCache;
		}

		internal static DirectPathRowPiece CreateLastPiece(bool bAllFastColumns, int len, int firstColIndex, int firstColDataOffset, int endColIndex, int endColDataOffset)
		{
			DirectPathRowPiece fromCache = GetFromCache();
			fromCache.InitLastPiece(bAllFastColumns, len, firstColIndex, firstColDataOffset, endColIndex, endColDataOffset);
			return fromCache;
		}

		internal static DirectPathRowPiece CreateFirstSplitPiece(bool bAllFastColumns, int len, int colIndex, int beginDataOffset, int endDataOffset)
		{
			DirectPathRowPiece fromCache = GetFromCache();
			fromCache.InitFirstSplitPiece(bAllFastColumns, len, colIndex, beginDataOffset, endDataOffset);
			return fromCache;
		}

		internal static DirectPathRowPiece CreateNextSplitPiece(bool bAllFastColumns, int len, int colIndex, int beginDataOffset, int endDataOffset)
		{
			DirectPathRowPiece fromCache = GetFromCache();
			fromCache.InitNextSplitPiece(bAllFastColumns, len, colIndex, beginDataOffset, endDataOffset);
			return fromCache;
		}

		internal static DirectPathRowPiece CreateLastSplitPiece(bool bAllFastColumns, int len, int colIndex, int beginDataOffset, int endDataOffset, bool bLastPiece)
		{
			DirectPathRowPiece fromCache = GetFromCache();
			fromCache.InitLastSplitPiece(bAllFastColumns, len, colIndex, beginDataOffset, endDataOffset, bLastPiece);
			return fromCache;
		}

		internal void InitRowPiece(bool bFastRow, int len, int firstColIndex, int beginColDataOffset, int endColIndex, int endColDataOffset)
		{
			m_bFastRow = bFastRow;
			m_totalColsLen = len;
			m_firstColIndex = firstColIndex;
			m_beginColDataOffset = beginColDataOffset;
			m_endColIndex = endColIndex;
			m_endColDataOffset = endColDataOffset;
			m_colCount = m_endColIndex - m_firstColIndex + 1;
		}

		internal void InitFullPiece(bool bAllFastColumns, int len, int firstColIndex, int firstColDataOffset, int endColIndex, int endColDataOffset)
		{
			InitRowPiece(bAllFastColumns, len, firstColIndex, firstColDataOffset, endColIndex, endColDataOffset);
			m_bFullRow = true;
			m_bFirstPiece = true;
			m_bLastPiece = true;
			m_bContinueFromPrevPiece = false;
			m_bContinueToNextPiece = false;
		}

		internal void InitFirstPiece(bool bAllFastColumns, int len, int firstColIndex, int firstColDataOffset, int endColIndex, int endColDataOffset)
		{
			InitRowPiece(bAllFastColumns, len, firstColIndex, firstColDataOffset, endColIndex, endColDataOffset);
			m_bFullRow = false;
			m_bFirstPiece = true;
			m_bLastPiece = false;
			m_bContinueFromPrevPiece = false;
			m_bContinueToNextPiece = false;
		}

		internal void InitNextPiece(bool bAllFastColumns, int len, int firstColIndex, int firstColDataOffset, int endColIndex, int endColDataOffset)
		{
			InitRowPiece(bAllFastColumns, len, firstColIndex, firstColDataOffset, endColIndex, endColDataOffset);
			m_bFullRow = false;
			m_bFirstPiece = false;
			m_bLastPiece = false;
			m_bContinueFromPrevPiece = false;
			m_bContinueToNextPiece = false;
		}

		internal void InitLastPiece(bool bAllFastColumns, int len, int firstColIndex, int firstColDataOffset, int endColIndex, int endColDataOffset)
		{
			InitRowPiece(bAllFastColumns, len, firstColIndex, firstColDataOffset, endColIndex, endColDataOffset);
			m_bFullRow = false;
			m_bFirstPiece = false;
			m_bLastPiece = true;
			m_bContinueFromPrevPiece = false;
			m_bContinueToNextPiece = false;
		}

		internal void InitFirstSplitPiece(bool bAllFastColumns, int len, int colIndex, int beginDataOffset, int endDataOffset)
		{
			InitRowPiece(bAllFastColumns, len, colIndex, beginDataOffset, colIndex, endDataOffset);
			m_bFullRow = false;
			m_bFirstPiece = colIndex == 0;
			m_bLastPiece = false;
			m_bContinueFromPrevPiece = false;
			m_bContinueToNextPiece = true;
		}

		internal void InitNextSplitPiece(bool bAllFastColumns, int len, int colIndex, int beginDataOffset, int endDataOffset)
		{
			InitRowPiece(bAllFastColumns, len, colIndex, beginDataOffset, colIndex, endDataOffset);
			m_bFullRow = false;
			m_bFirstPiece = false;
			m_bLastPiece = false;
			m_bContinueFromPrevPiece = true;
			m_bContinueToNextPiece = true;
		}

		internal void InitLastSplitPiece(bool bAllFastColumns, int len, int colIndex, int beginDataOffset, int endDataOffset, bool bLastPiece)
		{
			InitRowPiece(bAllFastColumns, len, colIndex, beginDataOffset, colIndex, endDataOffset);
			m_bFullRow = false;
			m_bFirstPiece = false;
			m_bLastPiece = bLastPiece;
			m_bContinueFromPrevPiece = true;
			m_bContinueToNextPiece = false;
		}

		internal void MarshalHeader(MarshallingEngine mEngine, int rowIndex)
		{
			short flags = Flags;
			mEngine.MarshalUB1(flags);
			if (flags == 60)
			{
				mEngine.MarshalNativeUB2(m_totalColsLen);
			}
			mEngine.MarshalUB1((short)m_colCount);
		}

		private static void MarshalColLength(MarshallingEngine mEngine, int length)
		{
			if (((length <= 250) ? 1 : 3) == 1)
			{
				mEngine.MarshalUB1((byte)length);
				return;
			}
			mEngine.MarshalUB1(254);
			mEngine.MarshalNativeUB2((short)length);
		}
	}
}
